package com.wwd.movie.login.service.impl;

import com.wwd.movie.login.domain.dto.LoginUser;
import com.wwd.movie.login.domain.entity.MovieUser;
import com.wwd.movie.login.mapper.MovieUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private MovieUserMapper movieUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        MovieUser user = movieUserMapper.selectByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        if (user.getStatus() == 1) {
            throw new RuntimeException("账号已禁用");
        }
        return new LoginUser(user); // 自定义封装实体，实现 UserDetails
    }
}
